using System;
using System.IO;


namespace NGCDS
{
    public class Rotater
    {
        private int rotateTimes = 4;

        public Rotater()
        {
            this.rotateTimes = NGCDS.Properties.Settings.Default.DEFAULT_ROTATER_TIMES;
        }

        public Rotater(int rotateTimes)
        {
            this.rotateTimes = rotateTimes;
        }

        public void rotateByDays(string file, int days)
        {
            if (File.Exists(file))
            {
                TimeSpan ts = DateTime.Now.Subtract(File.GetCreationTime(file));
                if (ts.TotalDays > days)
                {
                    rolling(file, rotateTimes);
                }
            }
        }

        public void rotateBySizes(string file, int sizes)
        {
            if (File.Exists(file))
            {
                FileInfo fileInfo = new FileInfo(file);
                if (fileInfo.Length > sizes)
                {
                    rolling(file, rotateTimes);
                }
            }
        }

        private void rolling(string file, int rtimes)
        {
            string src = null;
            string dest = null;
            for (int i = rtimes - 1; i > 0; i--)
            {
                src = file + "." + i;
                dest = file + "." + (i + 1);
                FileExtension.moveFile(src, dest);
            }
            src = file;
            dest = file + ".1";
            FileExtension.moveFile(src, dest);
        }
    }
}
